/**
 * 
 */
package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.OrderDto;
import com.scpii.universal.hibernate.dao.OrderManageDao;
import com.scpii.universal.service.OrderManageService;

/**
 * universalWeb
 * 
 * @author weny 2012-7-17下午02:22:31
 */
@Service
public class OrderManageServiceImpl extends BaseServiceImpl<OrderDto, Integer>
		implements OrderManageService {
	@Resource
	private OrderManageDao orderManageDao;

	@Resource
	public void setBaseDao(OrderManageDao orderManageDao) {
		super.setBaseDao(orderManageDao);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.scpii.universal.service.OrderService#getOrderRecode(java.util.Map)
	 */
	@Override
	public Integer getOrderRecode(OrderDto condition) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.OrderDto(t1.intOrderId ) ");
		hql.append(" from OrderTab t1 , ");
		hql.append("  ClientUserTab t2  ");
		hql.append(" where t1.intUserId=t2.intUserId ");
		hql.append(" and t1.intAppId=:intAppId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		// 查询条件
		if (condition != null) {
			if (condition.getVarUserName() != null
					&& !"".equals(condition.getVarUserName())) {
				hql.append(" and t2.varUserName like:varUserName");
				argsMap.put("varUserName", "%" + condition.getVarUserName()
						+ "%");
			}
			if (condition.getDatOrderDateStartTime() != null
					&& !"".equals(condition.getDatOrderDateStartTime())) {
				hql.append(" and t1.datOrderDatetime >=:datOrderDateStartTime ");
				argsMap.put("datOrderDateStartTime",
						condition.getDatOrderDateStartTime());
			}
			if (condition.getDatOrderDateEndTime() != null
					&& !"".equals(condition.getDatOrderDateEndTime())) {
				hql.append(" and t1.datOrderDatetime<=:datOrderDatetime ");
				argsMap.put("datOrderDatetime",
						condition.getDatOrderDateEndTime());
			}
			argsMap.put("intAppId", condition.getIntAppId());
		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);

		List<OrderDto> list = orderManageDao
				.hqlGetList(hql.toString(), argsMap);
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.scpii.universal.service.OrderService#getOrderListPage(java.util.Map,
	 * int, int, int)
	 */
	@Override
	public List<OrderDto> getOrderListPage(OrderDto condition, int pageNo,
			int pageStart, int pageRecordNo) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.OrderDto(t1.intOrderId,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.intAppId,");
		hql.append(" t1.dobOrderMoney,");
		hql.append(" t1.intOrderCount,");
		hql.append(" t1.datOrderDatetime,");
		hql.append("t1.intDeleteSign,");
		hql.append("t1.intOrderStatues,");
		hql.append(" t1.datUpdateDatetime,  ");
		hql.append(" t3.varContentName,  ");
		hql.append(" t2.varUserLoginName, ");
		hql.append(" t2.varUserPhonenum  ) ");
		hql.append(" from OrderTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3  ");
		hql.append(" where t1.intUserId=t2.intUserId");
		hql.append(" and t1.intContentId=t3.intContentId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t1.intAppId=:intAppId ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		if (condition != null) {
			if (condition.getVarUserName() != null
					&& !"".equals(condition.getVarUserName())) {
				hql.append(" and t2.varUserName like:varUserName");
				argsMap.put("varUserName", "%" + condition.getVarUserName()
						+ "%");
			}
			if (condition.getDatOrderDateStartTime() != null
					&& !"".equals(condition.getDatOrderDateStartTime())) {
				hql.append(" and t1.datOrderDatetime >=:datOrderDateStarTtime ");
				argsMap.put("datOrderDateStarTtime",
						condition.getDatOrderDateStartTime());
			}
			if (condition.getDatOrderDateEndTime() != null
					&& !"".equals(condition.getDatOrderDateEndTime())) {
				hql.append(" and t1.datOrderDatetime<=:datOrderDatetime ");
				argsMap.put("datOrderDatetime",
						condition.getDatOrderDateEndTime());
			}
			argsMap.put("intAppId", condition.getIntAppId());
		}
		return orderManageDao.findByHql(hql.toString(), pageNo, pageRecordNo,
				argsMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.scpii.universal.service.OrderService#getOrderInfo(java.lang.Integer)
	 */
	@Override
	public OrderDto getOrderInfo(Integer intOrderId) {

		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.OrderDto(t1.intOrderId,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.intAppId,");
		hql.append(" t1.dobOrderMoney,");
		hql.append(" t1.intOrderCount,");
		hql.append(" t1.datOrderDatetime,");
		hql.append("t1.intDeleteSign,");
		hql.append("t1.intOrderStatues,");
		hql.append(" t1.datUpdateDatetime,  ");
		hql.append(" t3.varContentName,  ");
		hql.append(" t2.varUserName,");
		hql.append(" t2.varUserPhonenum ) ");
		hql.append(" from OrderTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3  ");
		hql.append(" where t1.intUserId=t2.intUserId");
		hql.append(" and t1.intContentId=t3.intContentId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t1.intOrderId=:intOrderId ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intOrderId", intOrderId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<OrderDto> list = orderManageDao
				.hqlGetList(hql.toString(), argsMap);
		if (list != null) {
			return list.get(0);
		} else {
			return null;
		}
	}

}
